// myself/pages/my-training/rank/rank.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    params: {
      currentPage: 1,
      id: '',
      pageSize: 20,
      sortField: "",
      sortType: "",
    },
    hasFixed: true,
    hasFixedTwo: true,
    fields: [{
      prop: 'num',
      label: '排名',
      width: '100'
    },
    {
      prop: 'name',
      label: '姓名',
      width: '160'
    },
    {
      prop: 'orgName',
      label: '所属组织',
      width: '260'
    },
    {
      prop: 'avgRehearsalScore',
      label: '演练均分',
      sortable: true,
      width: '150'
    },
    {
      prop: 'avgPassScore',
      label: '通关均分',
      sortable: true,
      width: '150'
    }],
    list: [],
    total: 0,
    show: false,
    contentList: [{content: '1、数据统计截止时间为前一天24时'}, {content: '2、演练或通关分数只保留每次演练或通关的最高分，没有进行演练或通关的分数为0'}],

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const { coachingSessionId: id } = options
    this.setData({
      'params.id': id
    })
    this.getRank()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  getRank () {
    const { currentPage } = this.data.params 
    app.api.post({
      url: 'report_activity_activityCoachingRank',
      data: this.data.params
    }).then(res => {
      let { list } = this.data
      currentPage === 1 ? (list = res.list) : list.push(...res.list)
      this.setData({ list, total: res.total })
    })
  },
  getSortField (prop) {
    switch (prop) {
      case 'avgRehearsalScore':
        return 'rehearsal'
      case 'avgPassScore':
        return 'pass'
      
      default:
        break;
    }
  },
  upClick (e) {
    this.setData({
      'params.currentPage': 1,
      'params.sortField': this.getSortField(e.detail.row.prop),
      'params.sortType': 'asc',
    })
    this.getRank()
  },
  downClick (e) {
    this.setData({
      'params.currentPage': 1,
      'params.sortField': this.getSortField(e.detail.row.prop),
      'params.sortType': 'desc'
    })
    this.getRank()
  },
  loadMore () {
    const { total, list, params } = this.data
    const { currentPage } = params
    if (total > list.length) {
      this.setData({
        'params.currentPage': currentPage + 1
      })
      this.getRank()
    }
  },
  showDetail () {
    this.setData({
      show: !this.data.show
    })
  }
})